﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SMS_Server
{
    class Device_Controller
    {
        Thread th;

        public void StartController()
        {
            th = new Thread(StartControlling);
            th.Start();
        }

        public void Close()
        {
            if (th == null) return;
            try
            {
                th.Abort();
            }
            catch { }
        }

        private void StartControlling()
        {
            System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
            el.Log = "Application";
            el.MachineName = ".";
            el.Source = "SMS Server";
            el.WriteEntry("Starting device controller", System.Diagnostics.EventLogEntryType.Information);
            while (!Global.ShuttingDown)
            {
                try
                {
                    if (!Global.DeviceManager.SystemConfigured)
                    {
                        Global.Refresh_DeviceList();
                        string PortName = "";
                        foreach (GSMModem modem in Global.Devices)
                        {
                            if (modem.IMEINumber == Global.IMEI)
                            {
                                PortName = modem.PortName; break;
                            }
                        }
                        if (PortName == "")
                        {
                            el.WriteEntry("Selected device with IMEI:" + Global.IMEI + " has not been found", System.Diagnostics.EventLogEntryType.Error);
                            return;
                        }
                        if (!Global.DeviceManager.SMSConfig(PortName))
                        {
                            Thread.Sleep(5000);
                        } continue;
                    }
                    //send a message.....
                    if (Global.Outbox.Count > 0)
                    {
                        SMSMessage sms = Global.Outbox[0];
                        Global.Outbox.RemoveAt(0);
                        Global.DeviceManager.SendSMS(sms);
                        Thread.Sleep(100);
                    }
                    List<SMSMessage> smses = Global.DeviceManager.ReadIncommingSMS(GSMManager.ReadIncommingSMSType.REC_UNREAD);
                    for (int i = 0; i < smses.Count; i++)
                    {
                        SMSMessage sms = smses[i];
                        Global.Inbox.Add(sms);
                        Global.DeviceManager.DeleteExistingSMS(sms.index);
                    }
                    Thread.Sleep(100);
                    Global.DeviceManager.GetSignalStrength();
                    Thread.Sleep(100);
                }
                catch (Exception ex)
                {
                    el.WriteEntry("Error in device controller:"+ex.Message, System.Diagnostics.EventLogEntryType.Error);
                    Thread.Sleep(5000);
                }
            }
        }
    
    }
}
